﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;

namespace LolBot
{
    public class LolBot
    {
        public IRCConfig config;

        private TcpClient ircConnection;
        private NetworkStream ns;
        private StreamReader reader;
        private StreamWriter writer;

        private const String IDENT_PASSWORD = "trololol";

        protected static bool isDtek = false;

        private LolBotMessageParser messageParser;
        private LolBotCommandHandler commandhandler;

        public LolBot(IRCConfig config)
        {
            this.config = config;
        }

        public void Start()
        {
            try
            {
                ircConnection = new TcpClient(config.server, config.port);

                ns = ircConnection.GetStream();
                reader = new StreamReader(ns);
                writer = new StreamWriter(ns);

                commandhandler = new LolBotCommandHandler(reader, writer, config);
                messageParser = new LolBotMessageParser();

                SetupEvents();

                ConnectToServer();

				String getLine = null;

                JoinChannel();

                while (!(getLine = reader.ReadLine()).StartsWith(":" + config.server + " 376"))
                {
                    Console.WriteLine("Skipping: " + getLine);
                }

                JoinChannel();

				while ((getLine = reader.ReadLine()) != null) 
                {
					messageParser.Parse(config, getLine);
				}
            }
            catch 
            {
                Console.WriteLine("Communication error!");
            }
        }

        private void ConnectToServer()
        {
            try
            {
                writer.WriteLine("NICK " + config.nick);
                writer.WriteLine("USER " + config.user + " \"\" \"\" :" + config.name);
                writer.Flush();
            }
            catch
            {
                Console.WriteLine("Server connection errror!");
            }
        }

        private void JoinChannel()
        {
            try
            {
                writer.WriteLine("JOIN " + config.channel);
                writer.Flush();
            }
            catch
            {
                Console.WriteLine("Could not join channel!");
            }
        }

        private void Identify()
        {
            try
            {
                Console.WriteLine("Identifying with server...");
                writer.WriteLine("PRIVMSG NickServ :IDENTIFY " + config.nick + " " + IDENT_PASSWORD);
                writer.Flush();
            }
            catch
            {
                Console.WriteLine("Error while identifying with server!");
            }
        }

        private void SetupEvents()
        {
            messageParser.PingEvent += new LolBotEventHandler(commandhandler.OnPing);
            messageParser.JoinEvent += new LolBotEventHandler(commandhandler.OnJoin);
            messageParser.KickEvent += new LolBotEventHandler(commandhandler.OnKick);
            messageParser.RendezvousEvent += new LolBotEventHandler(commandhandler.OnRendezvous);
            messageParser.MesserschmidtEvent += new LolBotEventHandler(commandhandler.OnMesserschmidt);

            messageParser.ExitEvent += new LolBotEventHandler(commandhandler.OnExit);
            messageParser.OpEvent += new LolBotEventHandler(commandhandler.OnOp);
            messageParser.AddOpEvent += new LolBotEventHandler(commandhandler.OnAddOp);
            messageParser.RemoveOpEvent += new LolBotEventHandler(commandhandler.OnRemoveOp);
            messageParser.AddBadWordEvent += new LolBotEventHandler(commandhandler.OnAddBadWord);
            messageParser.RemoveBadWordEvent += new LolBotEventHandler(commandhandler.OnRemoveBadWord);
            messageParser.IgnoreEvent += new LolBotEventHandler(commandhandler.OnIgnore);
            messageParser.UnignoreEvent += new LolBotEventHandler(commandhandler.OnUnignore);
            messageParser.HelpEvent += new LolBotEventHandler(commandhandler.OnHelp);
            messageParser.DateEvent += new LolBotEventHandler(commandhandler.OnDate);
            messageParser.SingEvent += new LolBotEventHandler(commandhandler.OnSing);
            messageParser.StopSingingEvent += new LolBotEventHandler(commandhandler.OnStopSinging);
            messageParser.SayEvent += new LolBotEventHandler(commandhandler.OnSay);
            messageParser.MeEvent += new LolBotEventHandler(commandhandler.OnMe);
        }
    }
}
